
from amscms.ams.models import AdStats, Advertizer, Ad
from amscms.core.utils.request import render_to_response
from amscms.dict.models import Term
from django.contrib.auth.views import logout

"""
The index page of the AMS module/web application
"""
def index(request):
    context = {}
    tags = Term.objects.find_popular_terms()
    top_investors = Advertizer.objects.find_top_investor_advertizers()
    top_registrars = Advertizer.objects.find_top_registrar_advertizers()
    top_ads = Ad.objects.find_top_ads()
    context["tags"] = tags
    context["total_registered_ads"] = Ad.objects.find_registered_ads_count()
    context["total_published_ads"] = Ad.objects.find_published_ads_count()
    context["total_ad_views"] = AdStats.objects.find_total_ad_views()
    if len(top_investors) == 0:
        context["top_investor"] = ""
        context["top_investor_total"] = "0"
    else:
        context["top_investor"] = top_investors[0]["advertizer"]
        context["top_investor_total"] = top_investors[0]["total"]
    if len(top_registrars) == 0:
        context["top_registrar"] = ""
        context["top_registrar_total"] = "0"
    else:
        context["top_registrar"] = top_registrars[0]["advertizer"]
        context["top_registrar_total"] = top_registrars[0]["total"]
    if len(top_ads) == 0:
        context["ad"] = None
    else:
        context["ad"] = top_ads[0]["ad"]
    return render_to_response(request, "index.html", context)

def core_logout(request):
    return logout(request, next_page = "/")


